let loadingId;
export let request_post = (url, data, cbOptions, loading = true) => {
    const app = getApp();
    if (loading && !loadingId) {
        loadingId = setTimeout(() => {
            wx.showLoading({
                title: '加载中...',
            })
        }, 500);
    }
    const params = JSON.stringify(data);
    return new Promise((resolve, reject) => {
        wx.request({
            url: `https://www.wangkaotong.com/api${url}`,
            method: "POST",
            data: params,
            header: {
                "Content-Type": "application/json",
                "Student-Id": wx.getStorageSync('studentId') || '',
            },
            success: function (response) {
                if (loading) {
                    wx.hideLoading();
                    clearTimeout(loadingId) && (loadingId = null)
                }
                if (response.statusCode == 401 || response.statusCode == 445) return app.doLogin(cbOptions);
                if (response.statusCode != 200 || response.data.code != 10000) {
                    wx.showToast({
                        title: response.data.msg,
                        icon: 'none',
                        duration: 3000
                    })
                    return
                }
                return resolve(response.data.data);
            },
            fail: function (res) {
                if (loading) {
                    wx.hideLoading();
                    clearTimeout(loadingId) && (loadingId = null)
                }
                return reject(res.data);
            }
        })
    })
};

export let uploadFile = (url, filePath, app, loading = true) => {
    if (loading) {
        wx.showLoading({
            title: '加载中...',
        })
    }

    const token = wx.getStorageSync("token");
    const baseData = {
        token: token,
        version: app.globalData.versionCode
    }

    return new Promise((resolve, reject) => {
        wx.uploadFile({
            url: `http://localhost:58300${url}`,
            filePath: filePath,
            name: 'file',
            header: {
                "Content-Type": "application/json",
                'basicParams': JSON.stringify(baseData)
            },
            success(res) {
                const data = JSON.parse(res.data);
                if (loading) {
                    wx.hideLoading();
                }
                if (data.state == -1116 || data.state == -1100) {
                    app.doLogin();
                    return;
                }

                return resolve(data);
            },
            fail: function (res) {
                const data = JSON.parse(res.data);
                if (loading) {
                    wx.hideLoading();
                }
                return reject(data);
            }
        })
    })

}
export let getUrl = () => {
    var pages = getCurrentPages() //获取加载的页面  
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var url = currentPage.route //当前页面url  
    wx.setStorageSync('Router', `${url}`);
    var options = currentPage.options //如果要获取url中所带的参数可以查看options  
    if (options) {
        var urlWithArgs = url + '?'
        for (var key in options) {
            var value = options[key]
            urlWithArgs += key + '=' + value + '&'
        }
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)

    wx.setStorageSync('Router', `${urlWithArgs}`)
}